local tf = {}

local spi_id, pin_cs = 1, 20
local dir_path = "SD/"

local function tf_mount_mt()
    local tb = io.lsmount()
    local strmt = string.match(json.encode(tb), dir_path)
    log.info("fatfs", "lsmount", json.encode(tb))
    -- for k, v in pairs(tb) do
    --     log.info("fatfs", "lsmount", k, v)
    -- end
    if strmt then
        return true
    else
        local mt, _ = fatfs.mount(fatfs.SPI, dir_path, spi_id, pin_cs, 24 * 1000 * 1000)
        if mt == false or mt == false then
            log.info("fatfs", "mount fail")
            return false
        else
            return true
        end
    end
end

function tf.tf_init()
    fatfs.debug(1)
    spi.setup(spi_id, nil, 0, 0, pin_cs, 8 * 1000 * 1000)
    gpio.setup(pin_cs, 1)
    local mt = tf_mount_mt()
    -- local mt, _ = fatfs.mount(fatfs.SPI, dir_path, spi_id, pin_cs, 24 * 1000 * 1000)
    if mt then
        local data, err = fatfs.getfree(dir_path)
        if data then
            h485.Sensor_val.sd_flag = 1
            -- TF卡剩余空间MB
            -- h485.Sensor_val.sd_free = data["free_kb"]/1024
            log.info("fatfs", "getfree", json.encode(data))
        else
            log.info("fatfs", "err", err)
        end
    else
        log.info("fatfs", "mount fail")
    end
    -- fatfs.unmount("fatfs")
    log.info("fatfs", "unmount", fatfs.unmount(dir_path))
end

function tf.tf_write_data(per, data)
    local str = ""
    if h485.Sensor_val.sd_flag == 1 then
        -- local mt, _ = fatfs.mount(fatfs.SPI, "fatfs", spi_id, pin_cs, 24 * 1000 * 1000)
        local mt = tf_mount_mt()
        if mt then
            log.info("fatfs", "mount ok")
            local res, err = fatfs.getfree(dir_path)
            if res then
                log.info("fatfs", "getfree", json.encode(res))
                local sdCardFreeSize = res["free_kb"]
                if (io.exists(dir_path) == 1) and (sdCardFreeSize > 100) then
                    --第一个参数1表示sd卡
                    --第二个参数1表示返回的总空间单位为KB
                    -- local sdCardFreeSize = rtos.get_fs_free_size(1, 1)
                    log.info("sd card free size " .. sdCardFreeSize .. " KB")
                    h485.Sensor_val.sd_free = sdCardFreeSize / 1024
                    local t = os.date("*t")
                    local s = string.format("%04d-%02d-%02d", t.year, t.month, t.day)
                    local f = io.open("SD/" .. s .. ".txt", "a+")
                    if f then
                        s = string.format(" %04d-%02d-%02d %02d:%02d:%02d ", t.year, t.month, t.day,
                            t.hour, t.min, t.sec)
                        str = per .. s .. "RECV=" .. string.toHex(data) .. "\r\n"
                        log.info("[TF]->write success.\n")
                        f:write(str)
                        f:close()
                    end
                end
                -- io.unmount(io.SDCARD)
            else
                log.info("fatfs", "getfree", "err", err)
            end
            log.info("fatfs", "unmount", fatfs.unmount(dir_path))
        end
    end
end

return tf
